package com.temple.comfig;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.temple.common.exception.TempleGlobalExceptionHandler;
import com.temple.common.exception.TempleRestResponseErrorHandler;
import com.temple.common.mybatis.ModelMetaObjectHandler;
import com.temple.common.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Slf4j
@Configuration
public class AutoConfiguration {


    /**
     * xss过滤
     * body缓存
     *
     * @return
     */
//    @Bean
//    public FilterRegistrationBean XssFilter() {
//        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new XssFilter());
//        log.info("XssFilter [{}]", filterRegistrationBean);
//        return filterRegistrationBean;
//    }

    /**
     * 分页插件
     */
    @ConditionalOnMissingBean(PaginationInterceptor.class)
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        log.info("PaginationInterceptor [{}]", paginationInterceptor);
        return paginationInterceptor;
    }


    /**
     * Spring上下文工具配置
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SpringContextHolder.class)
    public SpringContextHolder springContextHolder() {
        SpringContextHolder holder = new SpringContextHolder();
        log.info("SpringContextHolder [{}]", holder);
        return holder;
    }

    /**
     * 统一异常处理配置
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(TempleGlobalExceptionHandler.class)
    public TempleGlobalExceptionHandler exceptionHandler() {
        TempleGlobalExceptionHandler exceptionHandler = new TempleGlobalExceptionHandler();
        log.info("ShopGlobalExceptionHandler [{}]", exceptionHandler);
        return exceptionHandler;
    }

//    /**
//     * ID生成器配置
//     *
//     * @param properties
//     * @return
//     */
//    @Bean
//    @ConditionalOnMissingBean(ShopIdGenProperties.class)
//    public SnowflakeIdGenerator snowflakeIdWorker(ShopIdGenProperties properties) {
//        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator(properties.getWorkId(), properties.getCenterId());
//        log.info("SnowflakeIdGenerator [{}]", snowflakeIdGenerator);
//        return snowflakeIdGenerator;
//    }


    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();
        //设置自定义ErrorHandler
        restTemplate.setErrorHandler(new TempleRestResponseErrorHandler());
        log.info("RestTemplate [{}]", restTemplate);
        return restTemplate;
    }

    /**
     * 自动填充模型数据
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(ModelMetaObjectHandler.class)
    public ModelMetaObjectHandler modelMetaObjectHandler() {
        ModelMetaObjectHandler metaObjectHandler = new ModelMetaObjectHandler();
        log.info("ModelMetaObjectHandler [{}]", metaObjectHandler);
        return metaObjectHandler;
    }
}
